	//
	//  JsLogDetailViewController.m
	//  JsUtilitySdk
	//
	//  Created by Gikki Ares on 2022/12/21.
	//

#import "JsLogDetailViewController.h"
#import "JsFileManager.h"
#import "JsMailManager.h"
#import "JsLogManager.h"
#import "JsColorManager.h"
#import "JsGeometryManager.h"
@interface JsLogDetailViewController ()<JsMailManagerDelegate>
{
	UITextView * mtv;
}
@end

@implementation JsLogDetailViewController

- (instancetype)init {
	if(self = [super init]) {
	
	}
	return self;
}

- (void)setContent:(NSString *)content {
	_content = content;
	mtv.text = content;
}

- (void)viewDidLoad {
	[super viewDidLoad];
		// Do any additional setup after loading the view.
	self.title = @"日志详情";
	
	
	NSMutableArray * marr_bbi = [NSMutableArray array];
	UIBarButtonItem * bbi_share = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(onClickShare)];
	[marr_bbi addObject:bbi_share];
	if(self.file) {
		UIBarButtonItem * bbi_clear = [[UIBarButtonItem alloc]initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(onClickClear)];
		[marr_bbi addObject:bbi_clear];
	}
	self.navigationItem.rightBarButtonItems = marr_bbi;
		//	mtv.bounces = NO;
		//	self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)displayFileContent {
	NSError * error;
	NSString * content = [[NSString alloc]
						  initWithContentsOfFile:self.file.filePath
						  encoding:NSUTF8StringEncoding
						  error:&error];
	if(error) {
		mtv.text = error.userInfo.description;
	}
	else {
		mtv.text = content;
	}
}

- (void)viewDidLayoutSubviews {
	mtv.frame = CGRectMake(0, JsGeometryManager.navigationBarBottom, JsGeometryManager.screenWidth, JsGeometryManager.screenHeight - JsGeometryManager.navigationBarBottom);
}

-(void)onClickShare {
	UIAlertController * ac = [UIAlertController alertControllerWithTitle:@"分享" message:@"请选择分享方式" preferredStyle:UIAlertControllerStyleActionSheet];
	UIAlertAction * aa_email = [UIAlertAction actionWithTitle:@"邮件发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		JsMailManager * mm = [JsMailManager sharedInstance];
		mm.delegte = self;
		[mm sendFile:self.file.filePath withConfig:[JsMailConfig jsConfig]];
	}];
	UIAlertAction * aa_share = [UIAlertAction actionWithTitle:@"系统分享" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		[self systemShareWithFile:self.file.filePath];
	}];
	UIAlertAction * aa_cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
	
		//	UIAlertAction * aa_delete = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:nil];
	
	[ac addAction:aa_email];
	[ac addAction:aa_share];
	[ac addAction:aa_cancel];
		//	[ac addAction:aa_delete];
	
	[self presentViewController:ac animated:YES completion:nil];
}

- (void)onClickClear {
	[JsFileManager emptyFile:self.file.filePath];
	[self displayFileContent];
}

- (void)renderVm {
	UIColor * clearColor = [UIColor clearColor];
	if(!mtv) {
		mtv = [[UITextView alloc]init];
		mtv.editable = NO;
			//导航栏下面还是不要显示比较好,否则滑动起来颜色不舒服.
		mtv.frame = CGRectMake(0, JsGeometryManager.navigationBarBottom, JsGeometryManager.screenWidth, JsGeometryManager.screenHeight - JsGeometryManager.navigationBarBottom);
		[self.view addSubview:mtv];
		mtv.backgroundColor = clearColor;
	}
	if(self.content) {
		mtv.text = self.content;
	}
	else if(self.file) {
		[self displayFileContent];
	}
	else {
		mtv.text = nil;
	}
	if(self.vm.backgroundColor) {
		self.view.backgroundColor = self.vm.backgroundColor;
		if([JsColorManager isColorDark:self.vm.backgroundColor]) {
			mtv.textColor = [UIColor whiteColor];
		}
		else {
			mtv.textColor = [UIColor blackColor];
		}
	}
}

- (void)jsMailManager:(JsMailManager *)manager didSendMailFailed:(NSString *)message {
	jslogi_fc(@"JsLogDetailViewController",nil, @"邮件发送失败,%@",message);
}

- (void)jsMailManager:(JsMailManager *)manager didSendMailSuccess:(NSString *)message {
	jslogi_fc(@"JsLogDetailViewController",nil, @"邮件发送成功!");
}

- (void)systemShareWithFile:(NSString *)filePath {
		// 获取压缩包数据
	NSArray *activityItems = @[[NSURL fileURLWithPath:filePath]];
	UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
		// 兼容ipad
		//	if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
		//		activityVC.popoverPresentationController.sourceView = Sub_Controller.view;
		//		activityVC.popoverPresentationController.sourceRect = CGRectMake(Sub_Controller.view.frame.size.width/2.0, Sub_Controller.view.frame.size.height/2.0, 1.0, 1.0);
		//		activityVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
		//	}
		// 去除一些不需要的图标选项
		//activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo];
		// 成功失败的回调block
	UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType,
																   BOOL completed, NSArray * __nullable returnedItems,
																   NSError * __nullable activityError) {
		if (completed) {
			jslogi_fc(@"JsLogDetailViewController",nil, @"日志分享成功!");
		}
		else {
			jslogi_fc(@"JsLogDetailViewController",nil, @"日志分享失败!");
		}
	};
	activityVC.completionWithItemsHandler = myBlock;
	[self presentViewController:activityVC animated:YES completion:nil];
}

@end
